package web.dao.impls;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import map.beans.WmsServerBean;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import web.dao.interfaces.WmsDaoInterface;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;


@Repository("WmsDao")
@Transactional(readOnly = true)
public class WmsDaoImpl implements WmsDaoInterface {
	private static Logger LOGGER = LoggerFactory.getLogger(WmsDaoImpl.class);
	
	@PersistenceContext
	private EntityManager entityManager;
	
	@Override
	@Transactional
	public Long save(WmsServerBean wms) {		
		try {
			System.out.println("........... Persisting Object  " + wms.getName() + "  .............."); 
			//wms = userExists(wms);
			entityManager.persist(wms);
			entityManager.flush();
		} catch (Exception e) {
			LOGGER.error(e.getMessage());	
		}
		return wms.getUrlId();
	}
	
	@Override
	@Transactional
	public Long update(WmsServerBean wms) {		
		try {
			System.out.println("........... Updating Object " + wms.getName() + "  .............."); 
			//wms = userExists(wms);
			entityManager.merge(wms);
			entityManager.flush();
		} catch (Exception e) {
			e.printStackTrace();		
		}
		return wms.getUrlId();
	}

	@Override
	public List<WmsServerBean> findAllWms() {
		TypedQuery<WmsServerBean> query = entityManager.createQuery("SELECT g FROM WmsServerBean g ORDER BY g.position", WmsServerBean.class);
		return query.getResultList();
	}
	
	
	
	@Override
	public boolean delete(WmsServerBean wms) {
		// check that it exists in the DB
		wms = entityManager.getReference(WmsServerBean.class, wms.getUrlId());
		boolean status = false;
		
		if (wms != null) {					
			entityManager.remove(wms);
			entityManager.flush();
			status = true;
		}
		return status;
	}

	@Override
	public List<WmsServerBean> reorderWmsServers(Map<Integer, Integer> wms) {		
		for (Map.Entry<Integer, Integer> e : wms.entrySet()) {
			System.out.println("Key: " + e.getKey() + "\tPosition: " + (e.getValue()));
			WmsServerBean server = entityManager.find( WmsServerBean.class, Long.parseLong(e.getKey().toString() ));
			server.setPosition(e.getValue());
			entityManager.persist(server);
			entityManager.flush();
		}		
		return findAllWms();
	}
	
	@Override
	public String getMapServersAsJson() {		
		List<WmsServerBean> mapServers = new ArrayList<WmsServerBean>();
		String json = "";
		try {
			mapServers = findAllWms();			
			
			Gson gson = new GsonBuilder().create();
			
			// convert list to json string
			json = gson.toJson(mapServers);			
			
		} catch (Exception e) {
			LOGGER.error(e.getMessage());
		}
		
		return json;
	}

	@Override
	public WmsServerBean getWmsServer(Long id) {
		WmsServerBean wms = entityManager.find(WmsServerBean.class, id);
		
		return wms;
	}	

}
